package com.steven.im.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import butterknife.BindView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.steven.putorefresh.BaseBothListener;
import com.steven.putorefresh.PullToBothLayout;
import com.steven.im.App;
import com.steven.im.R;
import com.steven.im.adapter.BidRecordAdapter;
import com.steven.im.base.BaseActivity;
import com.steven.im.entity.BaseEntity;
import com.steven.im.entity.BidRecordEntity;
import com.steven.im.net.MyCallback;
import com.steven.im.net.Task;
import com.steven.im.utils.TitleBuilder;
import com.steven.im.utils.ToastUtil;
import retrofit2.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Steven on 2019/2/15.
 * 竞价记录列表
 */
public class BidRecordActivity extends BaseActivity {
    @BindView(R.id.pullRefreshBoth)
    PullToBothLayout pullToBothLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private String issuedNo;
    private JSONArray array;
    private BidRecordAdapter bidRecordAdapter;
    private List<BidRecordEntity> list = new ArrayList<>();
    private int pageCount = 1;//当前 页数
    private int totalPage = 1;//总页数
    private int pageNo = 1;//默认是第一页
    private int pageSize = 10;//默认 加载条数
    private boolean isRefresh = false;//是否是下拉刷新
    private boolean isLoadMore = false;//是否是 上拉加载

    @Override

    public void setListen() {
        pullToBothLayout.setRefreshListener(new BaseBothListener() {
            @Override
            public void refresh() {
                isRefresh = true;
                pageNo = 1;
                pageSize = 10;
                getBidRecordList();
            }

            @Override
            public void loadMore() {
                if (pageCount < totalPage) {
                    isLoadMore = true;
                    pageNo++;
                    pageSize = 10;
                    getBidRecordList();
                } else {
                    pullToBothLayout.finishLoadMore();
                    ToastUtil.showShort(App.getContext(), "没有更多数据了！");
                }
            }
        });
    }

    @Override
    public void setTitleBarColor() {
        new TitleBuilder(this).setTitleText("出价记录")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }


    @Override
    public int intiLayout() {
        return R.layout.activity_bidrecord;
    }

    @Override
    public void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    @Override
    public void initData() {
        issuedNo = getIntent().getStringExtra("issuedNo");
        setData();
        getBidRecordList();
    }

    //初始化数据
    private void setData() {
        bidRecordAdapter = new BidRecordAdapter(BidRecordActivity.this, list);
        recyclerView.setAdapter(bidRecordAdapter);
    }

    //获取出价记录
    private void getBidRecordList() {
        Task.getApiService().getBidRecord(issuedNo, pageSize + "", pageNo + "").enqueue(new MyCallback<BaseEntity>(BidRecordActivity.this) {
            @Override
            protected void onSuccess(Response<BaseEntity> response) {
                pullToBothLayout.finishRefresh();
                pullToBothLayout.finishLoadMore();
                if ("0".equals(response.body().getCode())) {
                    if (response.body().getResult().containsKey("biddingRecodeList")) {
                        array = response.body().getResult().getJSONArray("biddingRecodeList");
                        JSONObject pagerObject = ((JSONObject) (JSON.toJSON(response.body().getResult().get("pager"))));
                        pageCount = Integer.parseInt(pagerObject.getString("pageNo"));
                        totalPage = Integer.parseInt(pagerObject.getString("totalPage"));
                        List<BidRecordEntity> newList = new ArrayList<>();
                        if (array.size() > 0) {
                            //中间过度的list
                            for (int i = 0; i < array.size(); i++) {
                                JSONObject jsonObject = JSON.parseObject(array.getString(i));
                                BidRecordEntity bidRecordEntity = new BidRecordEntity();
                                bidRecordEntity.setUrl(jsonObject.getString("userAvatar"));
                                bidRecordEntity.setPhone(jsonObject.getString("userName"));
                                bidRecordEntity.setStatus(jsonObject.getString("biddingStatus"));
                                bidRecordEntity.setAddress(jsonObject.getString("userCity"));
                                bidRecordEntity.setMoney("￥" + jsonObject.getString("biddingPrice"));
                                newList.add(bidRecordEntity);
                            }
                        }
                        if (isRefresh) {
                            isRefresh = false;
                            list.clear();
                            list.addAll(newList);
                        } else if (isLoadMore) {
                            isLoadMore = false;
                            list.addAll(newList);
                        } else {
                            list.addAll(newList);
                        }
                        bidRecordAdapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            protected void onFailure(Throwable t) {
                pullToBothLayout.finishRefresh();
                pullToBothLayout.finishLoadMore();
            }
        });
    }
}
